import { GET_HOME_LIST,PAGE_HOME_LIST } from './constants'
import axios from 'axios'
// const homeList = (result) =>({
// 	type: GET_HOME_LIST,
// 	articleList:result.articleList.data,
// 	page:result.page,
// 	toatl:result.toatl,
// 	pageSize:result.pageSize
// });


export const getHomeList=()=>async(dispatch,getState)=>{
	let { articleList }=getState().home;
	if( articleList.length ) return;
	
	await new Promise(resolve=>{
		axios.get(`http://www.mayunyi.top/api/articlelist/pages/?page=1&rows=20`).then(json=>{
			const data ={
				articleList:json.data.data,
				page:1,
				total:json.data.toatl ? json.data.toatl : 0,
				pageSize:20
			};
			resolve(data)
		})
	}).then(homeInfo=>{
		dispatch({
			type: GET_HOME_LIST,
			...homeInfo
		})
	})
};

export const pageList = (page,rows) => {
	return (dispatch) => {
		axios.get(`http://www.mayunyi.top/api/articlelist/pages/?page=${page}&rows=${rows}`).then(json=>{
			const data ={
				articleList:json.data.data,
				page:page,
				total:json.data.toatl ? json.data.toatl : 0,
				pageSize:rows
			};
			dispatch({
				type: PAGE_HOME_LIST,
				...data
			})
		})
	}

};
